<template>
  <el-card class="page-container">
    <template #header>
      <div class="header">
        <span>研究生培养管理</span>
        <div class="extra">
          <el-button type="primary" @click="confirmAutoFill">word自助填写</el-button>
          <el-button type="primary" @click="openDrawer">添加研究生培养信息</el-button>
        </div>
      </div>
    </template>
    <!-- 搜索表单 -->
    <el-form inline>
      <el-form-item label="学期：">
        <el-select placeholder="请选择" v-model="searchSemester">
          <el-option
            v-for="item in semesterTypes"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="getGraduateTrainings">搜索</el-button>
        <el-button @click="resetSearch">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 研究生培养列表 -->
    <el-table :data="graduateTrainings" style="width: 100%">
      <el-table-column label="学生人数" prop="students"></el-table-column>
      <el-table-column label="学期" prop="semester">
        <template #default="{ row }">
          <span>{{ getSemesterLabel(row.semester) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="学位级别" prop="degreeLevel">
        <template #default="{ row }">
          <span>{{ getDegreeLevelLabel(row.degreeLevel) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="100">
        <template #default="{ row }">
          <el-button :icon="Edit" circle plain type="primary" @click="editGraduateTraining(row)"></el-button>
          <el-button :icon="Delete" circle plain type="danger" @click="deleteGraduateTraining(row.id)"></el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="没有数据" />
      </template>
    </el-table>
    <!-- 分页条 -->
    <el-pagination
      v-model:current-page="pageNum"
      v-model:page-size="pageSize"
      :page-sizes="[5, 10, 20, 30]"
      layout="jumper, total, sizes, prev, pager, next"
      background
      :total="total"
      @size-change="onSizeChange"
      @current-change="onCurrentChange"
      style="margin-top: 20px; justify-content: flex-end"
    />
    <!-- 抽屉 -->
    <el-drawer
      v-model="visibleDrawer"
      title="添加研究生培养信息"
      direction="rtl"
      size="50%"
    >
      <el-form :model="graduateTrainingModel" label-width="100px">
        <el-form-item label="学生人数">
          <el-input v-model="graduateTrainingModel.students" placeholder="请输入学生人数"></el-input>
        </el-form-item>
        <el-form-item label="学期">
          <el-select v-model="graduateTrainingModel.semester" placeholder="请选择学期">
            <el-option
              v-for="item in semesterTypes"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="学位级别">
          <el-select v-model="graduateTrainingModel.degreeLevel" placeholder="请选择学位级别">
            <el-option label="研" value="研"></el-option>
            <el-option label="博" value="博"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="addOrUpdateGraduateTraining">提交</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>
  </el-card>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { Edit, Delete } from '@element-plus/icons-vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import {
  graduateTrainingListService,
  graduateTrainingAddService,
  graduateTrainingUpdateService,
  graduateTrainingDeleteService,
} from '@/api/graduateTraining';

// 数据模型
const graduateTrainings = ref([]);
const graduateTrainingModel = ref({
  id: null,
  students: '',
  semester: '',
  degreeLevel: '',
});
const visibleDrawer = ref(false);
const searchSemester = ref(null);
const semesterTypes = [
  { label: "第一学期", value: 1 },
  { label: "第二学期", value: 2 }
];
const pageNum = ref(1);
const pageSize = ref(10);
const total = ref(0);

// 获取研究生培养列表
const getGraduateTrainings = async () => {
  const params = {
    pageNum: pageNum.value,
    pageSize: pageSize.value,
    semester: searchSemester.value,
  };
  const { data } = await graduateTrainingListService(params);
  
  // 数据转换
  data.forEach(item => {
    item.semesterLabel = getSemesterLabel(item.semester);
    item.degreeLevelLabel = getDegreeLevelLabel(item.degreeLevel);
  });

  graduateTrainings.value = data;
  total.value = data.length;
};

// 添加或更新研究生培养
const addOrUpdateGraduateTraining = async () => {
  // 确保 semester 为整数
  graduateTrainingModel.value.semester = parseInt(graduateTrainingModel.value.semester, 10);

  if (graduateTrainingModel.value.id) {
    await graduateTrainingUpdateService(graduateTrainingModel.value);
    ElMessage.success('更新成功');
  } else {
    await graduateTrainingAddService(graduateTrainingModel.value);
    ElMessage.success('添加成功');
  }
  visibleDrawer.value = false;
  getGraduateTrainings();
};

// 删除研究生培养
const deleteGraduateTraining = async (id) => {
  await ElMessageBox.confirm('确认删除该研究生培养信息吗？', '提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning',
  });
  await graduateTrainingDeleteService(id);
  ElMessage.success('删除成功');
  getGraduateTrainings();
};

// 编辑研究生培养
const editGraduateTraining = (row) => {
  graduateTrainingModel.value = { ...row };
  visibleDrawer.value = true;
};

// 获取学期标签
const getSemesterLabel = (value) => {
  const semester = semesterTypes.find(item => item.value === value);
  return semester ? semester.label : '';
};

// 获取学位级别标签
const getDegreeLevelLabel = (value) => {
  return value === '研' ? '研究生' : value === '博' ? '博士生' : value;
};

// 分页变化
const onSizeChange = (size) => {
  pageSize.value = size;
  getGraduateTrainings();
};
const onCurrentChange = (num) => {
  pageNum.value = num;
  getGraduateTrainings();
};

// 重置搜索条件
const resetSearch = () => {
  searchSemester.value = null;
  getGraduateTrainings();
};

// 打开抽屉
const openDrawer = () => {
  graduateTrainingModel.value = {
    id: null,
    students: '',
    semester: '',
    degreeLevel: '',
  };
  visibleDrawer.value = true;
};
// 确认自助填写
const confirmAutoFill = () => {
  ElMessageBox.confirm('是否自助填写word文档？', '提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'info',
  }).then(() => {
    ElMessage.success('自助填写已确认');
    // 在此处添加自助填写的逻辑
  }).catch(() => {
    ElMessage.info('自助填写已取消');
  });
};


onMounted(() => {
  getGraduateTrainings();
});
</script>

<style lang="scss" scoped>
.page-container {
  min-height: 100%;
  box-sizing: border-box;

  .header {
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
}
</style>
